package org.hxyjs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.hxyjs.aop.SystemMail;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.Integral;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.IntegralService;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


@CrossOrigin
@RestController //@Controller注解是用spring容器生成 对象 默认对象名 integralController
 //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /integralController
public class IntegralController {
	@Resource
	IntegralService integralService;
	@GetMapping("/integrals")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<Integral> getAllIntegrals() throws Exception{
		System.out.println("查询全部");
		return integralService.getAllIntegrals();
	}

	@GetMapping("/integrals/{inno}")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<Integral> getIntegralByUno(@PathVariable("inno") String inno) throws Exception{
		System.out.println(inno + "查询成功");
		return integralService.getIntegralByUno(inno);
	}
	//@SystemMail(op = "Integral:addIntegral",queueName = "family.mail",module = "用户管理",methods = "用户注册")
	@PostMapping("/integrals")
	@ResponseBody
	public DTO<Integral> addIntegralByUno(@RequestBody Integral integral) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加成功");
		return integralService.addIntegralByUno(integral);
	}

	@DeleteMapping("/integrals/{inno}")
	@ResponseBody
	public DTO<Integral> deleteIntegral(@PathVariable("inno") String inno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println(inno+"删除成功");
		return integralService.deleteIntegral(inno);
	}

	@RequestMapping("/getIntegralsWithOrders/{inno}")
	@ResponseBody
	public DTO<Integral> getIntegralsWithOrders(@PathVariable("inno") String inno,HttpServletRequest request,HttpServletResponse response) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询学生选课111");
		DTO<Integral> dto = integralService.getIntegralsWithOrders(inno);
		saveSelectCourseCookieProcess(dto.getT(),request,response);
		return  dto;
	}
	@PatchMapping(value = "/integral",headers = {"content-type=application/json"},consumes = MediaType.APPLICATION_JSON_VALUE
	)//部分修改 //部分修改
	public DTO<Integral> updateIntegral(@RequestBody Integral integral) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改成功");
		return integralService.updateIntegral(integral);
	}
	@RequestMapping("/integrals/pageInfo")
public DTO<Integral> getIntegralsByPage(@SpringQueryMap PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询分页");
		return integralService.getIntegralsByPage(pageInfo);
	}
	public DTO<Integral> saveSelectCourseCookieProcess(Integral integral, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
		//cookie是以key value形式存放在浏览器的文本
		DTO dto = new DTO(200,"cookie保存成功");
//		if(integral!=null&&integral.getUno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			Optional<List<OrderForm>> orderFormsOp = Optional.ofNullable(integral.getOrderForms());
//			List<OrderForm> orderForms = null;
//			if (orderFormsOp.isPresent()&&orderFormsOp.get().size()>0) {
//				orderForms = new ArrayList<OrderForm>(integral.getOrderForms());
//
//				for (OrderForm orderForm : integral.getOrderForms()) {
//					Cookie orderFormsCookie = new Cookie("integral" + integral.getUno() + "_" + orderForm.getUrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderForm), "UTF-8"));
//
//					//integralCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//					orderFormsCookie.setPath("/");
//					orderFormsCookie.setDomain("localhost");
//					orderFormsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//					response.addCookie(orderFormsCookie);
//
//				}
//			}
//			integral.setOrderForms(null);
//			Cookie integralCookie = new Cookie("integral" + integral.getUno(), URLEncoder.encode(objectMapper.writeValueAsString(integral), "UTF-8"));
//			integralCookie.setPath("/");
//			integralCookie.setDomain("localhost");
//			integralCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(integralCookie);
//			integral.setOrderForms(orderForms);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
		return dto;
	}



	@RequestMapping("/activate/{inno}")
	@ResponseBody
	public DTO<Integral> activate(@PathVariable("inno") String inno) throws Exception{

		return integralService.activate(inno);
	}

	@SystemMail(op = "Integral:lettory",queueName = "family.mail",module = "中奖",methods = "中奖信息")
	@RequestMapping("/lettory/{inno}/{giftName}")
	@ResponseBody
	public DTO<Integral> lettory(@PathVariable("inno") String inno,@PathVariable("giftName")String giftName) throws Exception{

		return integralService.lettory(inno,giftName);
	}



}
